package com.example.lab.controller;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lab.entity.Lab1;
import com.example.lab.entity.LabVo;
import com.example.lab.entity.MyReturn;
import com.example.lab.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (Lab1)表控制层
 *
 * @author makejava
 * @since 2022-05-01 15:55:32
 */
@RestController
@RequestMapping("lab")
public class LabController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private LabService labService;
    @Resource
    private LabVoService labVoService;
    @Resource
    private ViewService viewService;
    @Resource
    private RoomService roomService;
    @Resource
    private DeviceService deviceService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param labVo 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public MyReturn selectAll(Integer limit, Integer page, LabVo labVo) {
        Integer page1=(page-1)*limit;
        Integer limit1=limit;
        List<LabVo> labVos = labVoService.queryAll(limit1, page1, labVo);

        /*Page<Lab1> labPage= new Page<>(page,limit);
        QueryWrapper<Lab1> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(lab.getLroom())){
            queryWrapper.like("Lroom",lab.getLroom());
        }
        if(ObjectUtil.isNotEmpty(lab.getLdevice())){
            queryWrapper.like("Ldevice",lab.getLdevice());
        }
        Page<Lab1> page1=this.labService.page(labPage,queryWrapper);*/
        Long count = labVoService.getCount();
        return super.success(labVos,count);
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public MyReturn selectOne(@PathVariable Serializable id) {
        return success(this.labService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param lab 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public MyReturn insert(@RequestBody Lab1 lab) {
        //计算时间差来确定金额
        Date lendtime = lab.getLendtime();
        Date lstarttime = lab.getLstarttime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = sdf.format(lstarttime);
        String e = sdf.format(lendtime);
        QueryWrapper<Lab1> queryWrapper=new QueryWrapper<>();
        Integer rId = lab.getLroom();
        Integer dId = lab.getLdevice();
        queryWrapper.eq("Lroom",rId);
        queryWrapper.eq("Ldevice",dId);
        queryWrapper.apply("UNIX_TIMESTAMP(Lstarttime) < UNIX_TIMESTAMP('" + s + "')");
        queryWrapper.apply("UNIX_TIMESTAMP(Lendtime) > UNIX_TIMESTAMP('" + s + "')");
        List<Lab1> labList = labService.list(queryWrapper);
        QueryWrapper<Lab1> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("Lroom",rId);
        queryWrapper1.eq("Ldevice",dId);
        queryWrapper1.apply("UNIX_TIMESTAMP(Lstarttime) < UNIX_TIMESTAMP('" + e+ "')");
        queryWrapper1.apply("UNIX_TIMESTAMP(Lendtime) > UNIX_TIMESTAMP('" + e + "')");
        List<Lab1> labList1 = labService.list(queryWrapper1);
        QueryWrapper<Lab1> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("Lroom",rId);
        queryWrapper2.eq("Ldevice",dId);
        queryWrapper2.apply("UNIX_TIMESTAMP(Lstarttime) >= UNIX_TIMESTAMP('" + s+ "')");
        queryWrapper2.apply("UNIX_TIMESTAMP(Lendtime) <= UNIX_TIMESTAMP('" + e + "')");
        List<Lab1> lab1List2 = labService.list(queryWrapper2);
        if(labList.size()>0||labList1.size()>0||lab1List2.size()>0){
            return failed("此时间段已被预约，请选择其他时间段或预约其他实验室");
        }else {
            boolean save = this.labService.save(lab);
            if (save) {
                return super.success(lab, 1L);
            } else {
                return super.failed(lab, 1L);
            }
        }
    }
    /**
     * 修改数据
     *
     * @param lab 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public  MyReturn update(@RequestBody Lab1 lab) {
        boolean update=this.labService.updateById(lab);
        if(update){
            return  super.success(lab,1L);
        }else {
            return  super.failed(lab,1L);
        }
    }
    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    public  MyReturn delete(@RequestParam("idList[]") List<Long> idList) {
        boolean b=labService.removeByIds(idList);
        if(b){
            return super.success(idList);
        }else {
            return super.failed(idList);
        }
    }
    @PostMapping("getView")
    public MyReturn view(){
        List<List<String>> view = viewService.getView();
        return super.success(view);
    }
}

